#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pymysql

def check_ymrs_data():
    """检查最新的YMRS数据"""
    try:
        # 数据库连接参数
        connection = pymysql.connect(
            host='localhost',
            port=3306,
            user='flask_dev',
            password='dev123456',
            database='flask_local',
            charset='utf8mb4'
        )
        
        cursor = connection.cursor()
        
        print("开始检查YMRS数据...")
        
        # 1. 检查最新的评估记录
        print("\n=== 最新的评估记录 ===")
        cursor.execute("""
            SELECT id, user_id, assessor_id, assessment_date, hamd_total, hama_total, ymrs_total, create_time
            FROM scale_assessment 
            ORDER BY create_time DESC 
            LIMIT 5
        """)
        assessments = cursor.fetchall()
        
        for assessment in assessments:
            print(f"ID: {assessment[0]}, 患者ID: {assessment[1]}, 医生ID: {assessment[2]}, 日期: {assessment[3]}")
            print(f"  HAMD: {assessment[4]}, HAMA: {assessment[5]}, YMRS: {assessment[6]}, 创建时间: {assessment[7]}")
        
        # 2. 检查最新的YMRS详细数据
        print("\n=== 最新的YMRS详细数据 ===")
        cursor.execute("""
            SELECT assessment_id, item1_mood_elevated, item2_activity, item3_sleep, 
                   item4_irritability, item5_speech_rate, item6_speech_pressured, item7_thought_content, 
                   item8_grandiosity, item9_distractibility, item10_hostility, item11_grandiosity_severity
            FROM scale_ymrs 
            ORDER BY id DESC 
            LIMIT 3
        """)
        ymrs_data = cursor.fetchall()
        
        for ymrs in ymrs_data:
            print(f"评估ID: {ymrs[0]}")
            print(f"  项目1-11分数: {ymrs[1:12]}")
            total = sum(ymrs[1:12]) if all(x is not None for x in ymrs[1:12]) else 0
            print(f"  计算总分: {total}")
        
        # 3. 检查患者信息
        print("\n=== 患者信息 ===")
        cursor.execute("""
            SELECT u.id, u.wechat_name, u.phone, up.real_name, up.gender, up.age
            FROM user u 
            LEFT JOIN user_patient up ON u.id = up.user_id 
            WHERE u.id = 11
        """)
        patient = cursor.fetchone()
        
        if patient:
            print(f"患者ID: {patient[0]}")
            print(f"微信名: {patient[1]}")
            print(f"手机号: {patient[2]}")
            print(f"真实姓名: {patient[3]}")
            print(f"性别: {patient[4]}")
            print(f"年龄: {patient[5]}")
        
        cursor.close()
        connection.close()
        
        print("\n数据库检查完成")
        
    except Exception as e:
        print(f"数据库连接失败: {e}")

if __name__ == "__main__":
    check_ymrs_data()
